{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e37ddf4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 5, 6)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#----------------------------------------------tuple----------------------------------------------\n",
    "tup = 4,5,6\n",
    "tup\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "38792c08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((4, 5, 6), (7, 8))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nested_tup = (4,5,6),(7,8)\n",
    "nested_tup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ffb5f716",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('S', 't', 'r', 'i', 'n', 'g')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#任意序列或迭代器转元组\n",
    "tuple([1,2,3]) #(1, 2, 3)\n",
    "tuple('String') #('S', 't', 'r', 'i', 'n', 'g')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f136f447",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 'S', 't', 'r', 'i', 'n', 'g')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用+连接元组\n",
    "(1, 2, 3) + ('S', 't', 'r', 'i', 'n', 'g')    #(1, 2, 3, 'S', 't', 'r', 'i', 'n', 'g')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "42c8c73e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#元组表达式赋值给变量，会对表达式进行拆包\n",
    "tup = (1,2,3)\n",
    "a,b,c = tup\n",
    "a  #1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "22c0802f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 4, 5]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用_表达不想要的变量\n",
    "values = 1,2,3,4,5\n",
    "a,b,*_ = values\n",
    "_  #[3, 4, 5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2a1c47c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#元组方法\n",
    "a = 1,2,3,3,3,5,3,6\n",
    "a.count(3) #4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e720a480",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([2, 3, 4, None], [6, 7, 8])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#---------------------------------------list---------------------------------------\n",
    "#使用[]或list函数定义\n",
    "a = [2,3,4,None]\n",
    "tup = (6,7,8)\n",
    "b = list(tup)\n",
    "\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d5efb170",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([2, 3, 4, None, 5], [6, 5, 7, 8])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#添加元素\n",
    "a.append(5)\n",
    "#insert涉及元素移动，性能较低\n",
    "b.insert(1,5)\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "50d1f533",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "pop index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn [11], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m#移除元素\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m val \u001b[38;5;241m=\u001b[39m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpop\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      3\u001b[0m b\u001b[38;5;241m.\u001b[39mremove(\u001b[38;5;241m5\u001b[39m)\n\u001b[0;32m      4\u001b[0m b\n",
      "\u001b[1;31mIndexError\u001b[0m: pop index out of range"
     ]
    }
   ],
   "source": [
    "#移除元素\n",
    "#移除指定位置\n",
    "val = a.pop(1)\n",
    "#移除匹配的第一个元素\n",
    "b.remove(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "201d3cfb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#判断存在\n",
    "5 in b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c88d4488",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, None, 6, 7, 8]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#扩展列表\n",
    "a.extend(b)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "47356c9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "bisect(a, 4)>>> 4\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#排序\n",
    "a = [4,2,5,1,3]\n",
    "a.sort()\n",
    "print(a)\n",
    "\n",
    "#二分搜索和已排序列表的维护\n",
    "import bisect\n",
    "print('bisect(a, 4)>>>', bisect.bisect(a, 4)) #寻找4应该被插入的位置\n",
    "bisect.insort(a, 0) #寻找并插入0应该的位置\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7aac8f45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 2, 3, 6, 3, 5, 6, 0, 1]\n",
      "[5, 6]\n",
      "[6, 3, 5, 6, 0, 1]\n"
     ]
    }
   ],
   "source": [
    "#切片\n",
    "seq = [7,2,3,7,5,6,0,1]\n",
    "\n",
    "#列表赋值给切片\n",
    "seq[3:4] =[6,3]\n",
    "print(seq)\n",
    "\n",
    "#负数切片，从最后一个进行索引，数数还是往后数的\n",
    "print(seq[-4:-2])\n",
    "print(seq[-6:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d23b921e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 3, 3, 6, 1]\n",
      "[1, 0, 6, 5, 3, 6, 3, 2, 7]\n"
     ]
    }
   ],
   "source": [
    "#step步长,每隔多少取一个值\n",
    "print(seq[::2])\n",
    "\n",
    "#通过设置-1的步长，达到翻转列表的效果\n",
    "print(seq[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "3f775360",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mapping>>> {0: 'foo', 1: 'bar', 2: 'baz'}\n",
      "sorted>>> [1, 2, 3, 6, 9]\n",
      "zipped>>> [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]\n",
      "[('foo', 'one', True), ('bar', 'two', False)]\n",
      "0:foo,one\n",
      "1:bar,two\n",
      "2:baz,three\n",
      "family_name>>> ('zhang', 'yang', 'li')\n",
      "pri_name>>> ('ziyang', 'yingying', 'yunlong')\n",
      "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n"
     ]
    }
   ],
   "source": [
    "#列表内建函数\n",
    "#enumerate遍历列表同时追踪索引\n",
    "some_list = ['foo', 'bar', 'baz']\n",
    "mapping = {}\n",
    "for index,val in enumerate(some_list):\n",
    "    mapping[index] = val\n",
    "print(\"mapping>>>\", mapping)\n",
    "\n",
    "#sorted返回已排序列表\n",
    "print('sorted>>>', sorted([3,6,1,2,9]))\n",
    "\n",
    "#zip将序列配对，新建成一个元组的列表\n",
    "seq1 = ['foo', 'bar', 'baz']\n",
    "seq2 = ['one','two','three']\n",
    "zipped = list(zip(seq1, seq2))\n",
    "print(\"zipped>>>\", zipped)\n",
    "#zipped长度由最小的配对序列决定\n",
    "seq3 = [True, False]\n",
    "print(list(zip(seq1, seq2, seq3)))\n",
    "#zip enumerate连用,同时输出多个序列的第123个元素\n",
    "for i, (a, b) in enumerate(list(zip(seq1, seq2))):\n",
    "    print('{0}:{1},{2}'.format(i,a,b))\n",
    "#zip给list(map)类型换轴\n",
    "ori = [('zhang','ziyang'), ('yang','yingying'), ('li', 'yunlong')]\n",
    "family_name, pri_name = zip(*ori)\n",
    "print(\"family_name>>>\", family_name)\n",
    "print(\"pri_name>>>\", pri_name)\n",
    "\n",
    "#reversed倒序，他是生成器，如果不遍历或list他，实际上不会产生倒序列表\n",
    "print(list(reversed(range(10))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a284f7b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
